package generator

import (
	v1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"

	"v8s-controller/internal/configuration"
)

type (
	LimitRangeGenerator struct {
		DefaultCPURequest resource.Quantity
		DefaultCPULimit   resource.Quantity

		DefaultRAMRequest resource.Quantity
		DefaultRAMLimit   resource.Quantity
	}
)

const (
	defaultLimitRangeName = "default"
)

var (
	LimitRangeMeta = metav1.TypeMeta{
		Kind:       "LimitRange",
		APIVersion: "v1",
	}
)

func NewLimitRangeGenerator(properties configuration.GeneratorProperties) *LimitRangeGenerator {
	return &LimitRangeGenerator{
		DefaultRAMRequest: resource.MustParse("50Mi"),
		DefaultRAMLimit:   resource.MustParse("50Mi"),
		DefaultCPURequest: resource.MustParse("10m"),
		DefaultCPULimit:   resource.MustParse("100m"),
	}
}

func (g *LimitRangeGenerator) Generate() []runtime.Object {
	return []runtime.Object{
		&v1.LimitRange{
			TypeMeta: LimitRangeMeta,
			ObjectMeta: metav1.ObjectMeta{
				Name: defaultLimitRangeName,
			},
			Spec: v1.LimitRangeSpec{
				Limits: []v1.LimitRangeItem{
					{
						Type: v1.LimitTypeContainer,
						DefaultRequest: map[v1.ResourceName]resource.Quantity{
							v1.ResourceCPU:    g.DefaultCPURequest,
							v1.ResourceMemory: g.DefaultRAMRequest,
						},
						Default: map[v1.ResourceName]resource.Quantity{
							v1.ResourceCPU:    g.DefaultCPULimit,
							v1.ResourceMemory: g.DefaultRAMLimit,
						},
					},
				},
			}},
	}
}
